﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BusinessObjects;
using DataAccess;
using QuanLyHopDong.Reports;

namespace QuanLyHopDong
{
    public partial class QuanLyHopDongDetailView : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler SaveChanged;
        private BindingList<NamHoc> blNamHoc;
        private BindingList<HopDongNhanVien> blHopDongNhanVien;

        private BusinessObjects.QuanLyHopDong _QuanLyHopDong;
        private HopDongNhanVien _HopDongNhanVien;
        private HopDongLamViec _HopDongLamViec;
        private HopDongLaoDong _HopDongLaoDong;
        private HopDongKhoan _HopDongKhoan;
        private NamHoc _NamHoc;

        const string HD_LAM_VIEC = "Hợp đồng làm việc";
        const string HD_LAO_DONG = "Hợp đồng lao động";
        const string HD_KHOAN = "Hợp đồng khoán";

        public QuanLyHopDongDetailView(BusinessObjects.QuanLyHopDong quanLyHopDong)
        {
            InitializeComponent();

            _QuanLyHopDong = quanLyHopDong;
            blNamHoc = new BindingList<NamHoc>(DataAccessObject.NamHoc.GetListNamHoc());

            bsNamHoc.DataSource = blNamHoc;

            LoadHopDong();
        }

        private void QuanLyHopDongDetailView_Load(object sender, EventArgs e)
        {
            string[] listFieldName = new string[] {"Oid", "TenNamHoc" };
            string[] listColumnName = new string[] {"ID", "Năm học" };
            AppGridView.InitGridView(gridViewNamHoc, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, true);
            AppGridView.ReadOnlyGridView(gridViewNamHoc);
            AppGridView.ShowField(gridViewNamHoc, listFieldName, listColumnName);

            listFieldName = new string[] { "SoHopDong", "NgayKy","NguoiKy.Ten","NguoiLaoDong.Ten","DiaDiemLamViec.TenBoPhan","ChucDanhChuyenMon","LoaiHopDong","HinhThucHopDong.TenHinhThucHopDong","TuNgay","DenNgay" };
            listColumnName = new string[] { "Số HĐ", "Ngày Ký", "Người ký","Người LĐ", "Địa điểm LV","Chức danh chuyên môn","Loại hợp đồng", "Hình thức hợp đồng","Từ ngày","Đến ngày" };
            AppGridView.InitGridView(gridViewHopDongNhanVien, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, true);
            AppGridView.ReadOnlyGridView(gridViewHopDongNhanVien);
            AppGridView.ShowField(gridViewHopDongNhanVien, listFieldName, listColumnName);
        }

        private void LoadHopDong()
        {
            blHopDongNhanVien = new BindingList<HopDongNhanVien>(DataAccessObject.HopDongNhanVien.GetListHopDongNhanVien(_QuanLyHopDong));
            bsHopDongNhanVien.DataSource = blHopDongNhanVien;

            gridViewHopDongNhanVien.Invalidate();
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void btnLuuVaDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Close();
        }

        private void gridNamHoc_EditValueChanged(object sender, EventArgs e)
        {
            if (gridNamHoc.GetSelectedDataRow() != null)
            {
                _NamHoc = gridNamHoc.GetSelectedDataRow() as NamHoc;
                LoadHopDong();
            }
        }

        private void btnHopDongLamViec_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _HopDongLamViec = new HopDongLamViec();
            _HopDongLamViec.QuanLyHopDong = _QuanLyHopDong;
            _HopDongLamViec.LoaiHopDong = HD_LAM_VIEC;

            HopDongLamViecDetailView detailView = new HopDongLamViecDetailView(_HopDongLamViec);
            detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
            detailView.ShowDialog();
        }

        private void detailView_SaveChanged(object sender, EventArgs e)
        {
            gridHopDongNhanVien.Invalidate();
        }

        private void btnHopDongLaoDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _HopDongLaoDong = new HopDongLaoDong();
            _HopDongLaoDong.QuanLyHopDong = _QuanLyHopDong;
            _HopDongLaoDong.LoaiHopDong = HD_LAO_DONG;

            HopDongLaoDongDetailView detailView = new HopDongLaoDongDetailView(_HopDongLaoDong);
            detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
            detailView.ShowDialog();
        }

        private void btnHopDongKhoan_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _HopDongKhoan = new HopDongKhoan();
            _HopDongKhoan.QuanLyHopDong = _QuanLyHopDong;
            _HopDongKhoan.LoaiHopDong = HD_KHOAN;

            HopDongKhoanDetailView detailView = new HopDongKhoanDetailView(_HopDongKhoan);
            detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
            detailView.ShowDialog();
        }

        private void gridViewHopDongNhanVien_DoubleClick(object sender, EventArgs e)
        {
            _HopDongNhanVien = gridViewHopDongNhanVien.GetFocusedRow() as HopDongNhanVien;
            if (_HopDongNhanVien != null)
            {
                
                ThamChieuDuLieu thamChieu = DataAccessObject.ThamChieuDuLieu.GetThamChieuDuLieu(_HopDongNhanVien.Oid, typeof(HopDong).ToString());
                if (thamChieu != null)
                {
                    if (thamChieu.Con.TenKieuDuLieu == typeof(HopDongLamViec).ToString())
                    {
                        HopDongLamViec hopDongLamViec = DataAccess.DataAccessObject.HopDongLamViec.GetHopDongLamViec(_HopDongNhanVien.Oid);
                        HopDongLamViecDetailView hopDongLamViecDetailView = new HopDongLamViecDetailView(hopDongLamViec);

                        hopDongLamViecDetailView.ShowDialog();
                    }
                    else if (thamChieu.Con.TenKieuDuLieu == typeof(HopDongLaoDong).ToString())
                    {
                        HopDongLaoDong hopDongLaoDong = DataAccess.DataAccessObject.HopDongLaoDong.GetHopDongLaoDong(_HopDongNhanVien.Oid);
                        HopDongLaoDongDetailView hopDongLaoDongDetailView = new HopDongLaoDongDetailView(hopDongLaoDong);
                        hopDongLaoDongDetailView.ShowDialog();
                    }
                    else if (thamChieu.Con.TenKieuDuLieu == typeof(HopDongKhoan).ToString())
                    {
                        HopDongKhoan hopDongKhoan = DataAccess.DataAccessObject.HopDongKhoan.GetHopDongKhoan(_HopDongNhanVien.Oid);
                        HopDongKhoanDetailView hopDongKhoanDetailView = new HopDongKhoanDetailView(hopDongKhoan);
                        hopDongKhoanDetailView.ShowDialog();
                    }
                }
            }
        }

        private void btnInHopDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            BindingList<HopDongLamViec> hdList = new BindingList<HopDongLamViec>();
            HopDongLamViec hdlv = new HopDongLamViec();
            hdlv.SoHopDong = "001/HĐLV-CNTĐ-NS";
            hdlv.TuNgay = DateTime.Today;
            hdlv.DenNgay = DateTime.Today.AddMonths(12);
            hdlv.NguoiKy = DataAccessObject.NhanVien.GetNhanVien(5);

            hdList.Add(hdlv);

            rptHopDongLamViec rpt = new rptHopDongLamViec();
            rpt.DataSource = hdList;
            rpt.ShowPreview();
            //show designer
            //rpt.ShowDesigner();
            
        }

        void OpenNewForm()
        {

        }

        private void gridHopDongNhanVien_DoubleClick(object sender, EventArgs e)
        {
            HopDongNhanVien hopDongNhanVien = gridViewHopDongNhanVien.GetFocusedRow() as HopDongNhanVien;
            HopDongKhoan hopDongKhoan = new HopDongKhoan();
            hopDongKhoan = DataAccessObject.HopDongKhoan.GetHopDongKhoan(hopDongNhanVien.Oid);

            if (hopDongNhanVien.LoaiHopDong == HD_LAM_VIEC)
            {
                _HopDongLamViec = DataAccessObject.HopDongLamViec.GetHopDongLamViec(hopDongNhanVien.Oid);
                HopDongLamViecDetailView detailView = new HopDongLamViecDetailView(_HopDongLamViec);
                detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
                detailView.ShowDialog();

            }
            else if (hopDongNhanVien.LoaiHopDong == HD_LAO_DONG)
            {
                _HopDongLaoDong = DataAccessObject.HopDongLaoDong.GetHopDongLaoDong(hopDongNhanVien.Oid);
                HopDongLaoDongDetailView detailView = new HopDongLaoDongDetailView(_HopDongLaoDong);
                detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
                detailView.ShowDialog();
            }
            else if (hopDongNhanVien.LoaiHopDong == HD_KHOAN)
            {
                _HopDongKhoan = DataAccessObject.HopDongKhoan.GetHopDongKhoan(hopDongNhanVien.Oid);
                HopDongKhoanDetailView detailView = new HopDongKhoanDetailView(_HopDongKhoan);
                detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
                detailView.ShowDialog();
            }

        }
    }
}